home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / msysjour / vol06 / 03 / wintro6 / fileopen.c < prev    next >
C/C++ Source or Header  |  1991-05-01  |  5KB  |  175 lines

  1. /*================================================================*/
  2. /*                                                                */
  3. /* File    : FILEOPEN.C                                           */
  4. /*                                                                */
  5. /* Purpose : Standard file-open dialog box processing. Taken from */
  6. /*           sample code in the Windows 3.0 SDK.                  */
  7. /*                                                                */
  8. /* History :                                                      */
  9. /*                                                                */
  10. /*================================================================*/
  11.  
  12. #include <string.h>
  13. #include <windows.h>
  14. #include "stock.h"
  15.  
  16. char FileName[128];
  17. char PathName[128];
  18. char OpenName[128];
  19. char DefPath[128];
  20. char DefSpec[13] = "*.sto";
  21. char DefExt[] = ".sto";
  22. char str[255];
  23.  
  24.  
  25. HANDLE FAR PASCAL OpenDlg(hDlg, message, wParam, lParam)
  26.   HWND hDlg;
  27.   unsigned message;
  28.   WORD wParam;
  29.   LONG lParam;
  30. {
  31.   WORD index;
  32.   PSTR pTptr;
  33.   HANDLE hFile;
  34.  
  35.   switch (message)
  36.   {
  37.     case WM_COMMAND:
  38.       switch (wParam)
  39.       {
  40.         case IDC_LISTBOX:
  41.            switch (HIWORD(lParam))
  42.            {
  43.              case LBN_SELCHANGE:
  44.                if (!DlgDirSelect(hDlg, str, IDC_LISTBOX))
  45.                {
  46.                  SetDlgItemText(hDlg, IDC_EDIT, str);
  47.                  SendDlgItemMessage(hDlg,
  48.                                     IDC_EDIT,
  49.                                     EM_SETSEL,
  50.                                     NULL,
  51.                                     MAKELONG(0, 0x7fff));
  52.                }
  53.                else
  54.                {
  55.                  strcat(str, DefSpec);
  56.                  DlgDirList(hDlg, str, IDC_LISTBOX,IDC_PATH,0x4010);
  57.                }
  58.                break;
  59.  
  60.              case LBN_DBLCLK:
  61.                goto openfile;
  62.            }
  63.            return TRUE;
  64.  
  65.          case IDOK:
  66. openfile:
  67.            GetDlgItemText(hDlg, IDC_EDIT, OpenName, 128);
  68.            if (strchr(OpenName, '*') || strchr(OpenName, '?'))
  69.            {
  70.              SeparateFile(hDlg, (LPSTR) str, (LPSTR) DefSpec,
  71.                                              (LPSTR) OpenName);
  72.              if (str[0])
  73.                strcpy(DefPath, str);
  74.              ChangeDefExt(DefExt, DefSpec);
  75.              UpdateListBox(hDlg);
  76.              return TRUE;
  77.            }
  78.            if (!OpenName[0])
  79.            {
  80.              MessageBox(hDlg, "No filename specified.", NULL, 
  81.                               MB_OK | MB_ICONHAND);
  82.              return TRUE;
  83.            }
  84.  
  85.            AddExt(OpenName, DefExt);
  86.  
  87.            /* The routine to open the file would go here, and the */
  88.            /* handle would be returned instead of NULL.           */
  89.            StockFileRead((LPSTR) OpenName);
  90.  
  91.            EndDialog(hDlg, hFile);
  92.            return (TRUE);
  93.  
  94.           case IDCANCEL:
  95.             EndDialog(hDlg, NULL);
  96.             return (TRUE);
  97.           }
  98.           break;
  99.  
  100.         case WM_INITDIALOG:                        
  101.             UpdateListBox(hDlg);
  102.             SetDlgItemText(hDlg, IDC_EDIT, DefSpec);
  103.             SendDlgItemMessage(hDlg,               
  104.                 IDC_EDIT,                          
  105.                 EM_SETSEL,                         
  106.                 NULL,                              
  107.                 MAKELONG(0, 0x7fff));              
  108.             SetFocus(GetDlgItem(hDlg, IDC_EDIT));
  109.             return (FALSE);
  110.     }
  111.     return FALSE;
  112. }
  113.  
  114.  
  115. void UpdateListBox(hDlg)
  116.   HWND hDlg;
  117. {
  118.   strcpy(str, DefPath);
  119.   strcat(str, DefSpec);
  120.   DlgDirList(hDlg, str, IDC_LISTBOX, IDC_PATH, 0x4010);
  121.   SetDlgItemText(hDlg, IDC_EDIT, DefSpec);
  122. }
  123.  
  124.  
  125. void ChangeDefExt(Ext, Name)
  126.   PSTR Ext, Name;
  127. {
  128.   PSTR pTptr;
  129.  
  130.   pTptr = Name;
  131.   while (*pTptr && *pTptr != '.')
  132.     pTptr++;
  133.   if (*pTptr)
  134.     if (!strchr(pTptr, '*') && !strchr(pTptr, '?'))
  135.       strcpy(Ext, pTptr);
  136. }
  137.  
  138.  
  139. void SeparateFile(hDlg, lpDestPath, lpDestFileName, lpSrcFileName)
  140.   HWND hDlg;
  141.   LPSTR lpDestPath, lpDestFileName, lpSrcFileName;
  142. {
  143.   LPSTR lpTmp;
  144.   char  cTmp;
  145.  
  146.   lpTmp = lpSrcFileName + (long) lstrlen(lpSrcFileName);
  147.   while (*lpTmp != ':' && *lpTmp != '\\' && lpTmp > lpSrcFileName)
  148.     lpTmp = AnsiPrev(lpSrcFileName, lpTmp);
  149.   if (*lpTmp != ':' && *lpTmp != '\\')
  150.   {
  151.     lstrcpy(lpDestFileName, lpSrcFileName);
  152.     lpDestPath[0] = 0;
  153.     return;
  154.   }
  155.   lstrcpy(lpDestFileName, lpTmp + 1);
  156.   cTmp = *(lpTmp + 1);
  157.   lstrcpy(lpDestPath, lpSrcFileName);
  158.   *(lpTmp + 1) = cTmp;
  159.   lpDestPath[(lpTmp - lpSrcFileName) + 1] = 0;
  160. }
  161.  
  162.  
  163. void AddExt(Name, Ext)
  164.   PSTR Name, Ext;
  165. {
  166.   PSTR pTptr;
  167.  
  168.   pTptr = Name;
  169.   while (*pTptr && *pTptr != '.')
  170.     pTptr++;
  171.   if (*pTptr != '.')
  172.     strcat(Name, Ext);
  173. }
  174.  
  175.